<?php
$dbname = 'ncdb';
$dbhost = 'cougar.wou.edu';
$dbuser = 'ncdb';
$dbpass = 'bdcn';
$db = mysql_connect($dbhost,$dbuser,$dbpass);

function fetchText($entry,$searchCol) {
	global $dbname, $db, $db_selected;
	$sql = "SELECT $searchCol from ncdb_Resources WHERE $searchCol LIKE '$entry%'";
	//echo $sql;
	$result = mysql_db_query($dbname,$sql);
	
	$returnString = '<ul>';
	$i=0;
	while ($value = mysql_fetch_assoc($result)) {
		if (($i % 2) > 0) { $class = 'blueRow'; }
		else { $class = ''; }	
		$colText = $value[$searchCol];
		$returnString .= '<li onClick="sendDataToField(this);" id="liDataRow'.$i.'" class="'.$class.'">'.$colText.'</li>';
		$i++;
	}
	$returnString .= '</ul>';
	return $returnString;
}	

function getColumnData($column,$sortBy=null,$acdc=null) {
	global $dbname, $db, $db_selected;
	
	if (empty($sortBy) || ($sortBy == 'undefined') ) {
		$sortBy = 'SearchCol';
	}
	if (empty($acdc) || ($acdc == 'undefined') ) {
		$acdc = 'ASC';
	}
	if ($acdc == 'ASC') {
		$linkACDC = 'DESC';
	} else {
		$linkACDC = 'ASC';
	}
	
	$searchCol = trim($column);
	if ($searchCol == 'Category') {
		$sql = "SELECT COUNT(LRC.CID) AS CountCol, LTRIM(MC.CatTitle) AS SearchCol
				FROM ncdb_LinkResourceCats AS LRC
				LEFT JOIN ncdb_MainCat AS MC ON MC.CatID = LRC.CID
				GROUP BY LRC.CID
				ORDER BY $sortBy $acdc";
	} elseif ($searchCol == 'Subject') {
		$sql = "SELECT COUNT(LRS.SID) AS CountCol, LTRIM(MC.CatTitle) AS SearchCol
				FROM ncdb_LinkResourceSubject AS LRS
				LEFT JOIN ncdb_MainCat AS MC ON MC.CatID = LRS.SID
				GROUP BY LRS.SID
				ORDER BY $sortBy $acdc";
	} elseif ($searchCol == 'Sets') {
		$sql = "SELECT COUNT(SSLN.SIID) AS CountCol,SS.Title AS SearchCol,SS.SID AS SSID,SS.SetDesc,SS.DateCreate,SS.Public
			FROM ncdb_SavedSets SS
			LEFT JOIN ncdb_SavedSetLink SSLN ON SSLN.SID = SS.SID
			WHERE
				(SS.Public = 1 OR
				(SS.Public = 0 AND Userid = '$userid')) AND
				SS.Temp <> 1 AND SS.ReportOnly <> 1 AND Type = 'Resource'			
			GROUP BY SS.SID			
			ORDER BY SS.Title
			";
	} else {	
		$sql = "SELECT COUNT($searchCol) AS CountCol,$searchCol AS SearchCol 
				FROM ncdb_Resources 
				GROUP BY SearchCol
				ORDER BY $sortBy $acdc
				";
	}	
	$result = mysql_db_query($dbname,$sql);
	if (mysql_num_rows($result)>0) {
		$i=0;
		$colString = '<table class="dataTable" style="width: 100%;">';
		$colString .= '<tr>';
		$colString .= '<th class="blueTH" onClick="switchColumn(\''.$searchCol.'\',\'CountCol\',\''.$linkACDC.'\'); return false;" style="cursor: pointer;">Count</th>';
		$colString .= '<th class="blueTH" onClick="switchColumn(\''.$searchCol.'\',0,\''.$linkACDC.'\'); return false;" style="cursor: pointer;">'.$searchCol.'</th>';
		$colString .= '</tr>';
		while ($value = mysql_fetch_assoc($result)) {
			if (($i % 2) > 0) { $class = 'altRow'; }
			else { $class = 'noRowClass'; }	
			if (!empty($value['SearchCol'])) {
				$colString .= '<tr class="'.$class.'">';
				$colString .= '<td style="width: 15%;">'.$value['CountCol'].'</td>';
				$colString .= '<td onClick="sendDataToField(this);" id="dataRow'.$i.'" style="width: 85%; cursor: pointer;">'.$value['SearchCol'].'</td>';
				$colString .= '</tr>';
				$i++;
			}	
		}		
		$colString .= '</table>';
	}	
	return $column.'~|~'.$colString;
}

if ($_GET[fetch] == 1) {
	$entry = mysql_real_escape_string($_REQUEST['autocomplete_parameter']);
	$searchCol = $_REQUEST['searchCol'];
	echo fetchText($entry,$searchCol);
}

?>